<template>
  <view class="index" :style="{overflow:'hidden',height:pageHeight}">
    <!-- 搜索 -->
    <search @search="disScroll" />
    <!-- 轮播图 -->
    <view class="slider">
      <swiper
        autoplay
        interval="2000"
        circular
        indicator-dots
        indicator-color="rgba(255,255,255,1)"
        indicator-active-color="rgba(255,255,255,.6)"
      >
        <swiper-item v-for="item in swiper" :key="item.goods_id">
          <navigator :url="'/packone/goods/index?id='+item.goods_id">
            <image :src="item.image_src" />
          </navigator>
        </swiper-item>
      </swiper>
    </view>
    <!-- 功能导航 -->
    <view class="navs">
      <navigator
        :open-type="item.open_type || 'navigate'"
        :url="item.navigator_url?'/pages/category/index':'/packone/list/index?query='+item.name"
        v-for="(item,index) in nav"
        :key="index"
      >
        <image :src="item.image_src" />
      </navigator>
    </view>
    <!-- 栏目楼层 -->
    <view class="floors">
      <!-- 1 -->
      <!-- 遍历楼层 -->
      <view class="floor" v-for="(item,i) in floors" :key="i">
        <!-- title -->
        <view class="ftitle">
          <image :src="item.floor_title.image_src" />
        </view>
        <!-- pics -->
        <view class="fitem">
          <!-- 遍历图片 -->
          <navigator
            :url="`/packone/list/index?query=${it.name}`"
            :key="j"
            v-for="(it, j) in item.product_list"
          >
            <image :src="it.image_src" />
          </navigator>
        </view>
      </view>
    </view>
    <!-- 底部提示 -->
    <view class="end">
      <text>我是有底线的！</text>
    </view>
    <!-- 回到顶部 -->
    <view v-if="isShowTop" @click="goTop" class="goTop icon-top"></view>
  </view>
</template>

<script>
// 导入组件
import search from "@/components/search"
// 测试=》调用封装的request
// import request from '@/utils/request'
// console.log('r', request)
export default {
  data () {
    return {
      pageHeight: "auto",
      // 轮播图
      swiper: [],
      // 导航
      nav: [],
      // 楼层
      floors: [],
      // 滚动高度
      scrollTop: 0
    }
  },
  computed: {
    // 大于半屏幕高度显示回顶部按钮
    isShowTop () {
      return this.scrollTop > this.wh
    }
  },
  // 注册组件
  components: {
    search
  },
  onReachBottom () {
    console.log('钩子：页面到底部了。。。')
  },
  // 页面滚动执行
  onPageScroll (opt) {
    // console.log(opt)
    this.scrollTop = opt.scrollTop
  },
  // vue=>created
  onLoad () {
    // 获取屏幕高度 =》存储到this上
    this.wh = uni.getSystemInfoSync().windowHeight / 2
    console.log(this.wh)
    this.getSwiperData()
    this.getNavData()
    this.getFloorData()
  },
  // 用户拖动触发=》下拉刷新的钩子函数
  onPullDownRefresh () {
    console.log('开始刷新...')
    // // js=》触发下拉刷新 =》不能调用js开启=》会死循环
    // uni.startPullDownRefresh()
    // Promise.all()
    // 1. 批量执行Promise方法
    // 2. 执行完=》返回Promise =>then=〉拿到
    // 批量执行的Promise方法resolve的结果
    Promise.all([this.getSwiperData(),
    this.getNavData(),
    this.getFloorData()]).then((res) => {
      console.log('数组中所有Promise都执行完了：', res)
      // uni.stopPullDownRefresh()
    })

  },
  methods: {
    // 点击回顶部
    /**
     * 1. 获取滚动的高度
     * 2. 高度大于屏幕高度的一半 =》显示回顶部按钮
     */
    goTop () {
      uni.pageScrollTo({
        scrollTop: 0,
        duration: 300
      })
    },
    // 搜索时禁止页面滚动
    disScroll (e) {
      this.pageHeight = e
    },
    // 获取轮播图数据
    async getSwiperData () {
      let { msg: { status }, data } = await this.request({
        url: '/api/public/v1/home/swiperdata',
      })
      if (status === 200) {
        this.swiper = data
      }
    },
    // 获取导航数据
    async getNavData () {
      let { msg: { status }, data } = await this.request({
        url: '/api/public/v1/home/catitems',
      })
      if (status === 200) {
        this.nav = data
      }
    },
    // 获取楼层数据
    async getFloorData () {
      let { msg: { status }, data } = await this.request({
        url: '/api/public/v1/home/floordata',
      })
      if (status === 200) {
        // console.log(data)
        this.floors = data
      }
    }
  }
};
</script>

<style lang="scss">
.index {
  // 轮播
  .slider {
    swiper,
    image {
      width: 750rpx;
      height: 340rpx;
    }
  }
  // 功能导航
  .navs {
    display: flex;
    padding: 30rpx;
    background: #fff;
    justify-content: space-between;
    navigator {
      width: 128rpx;
      height: 140rpx;
    }
  }

  // 栏目楼层
  .floor {
    .ftitle {
      padding-top: 30rpx;
      background: #f4f4f4;
      image {
        width: 750rpx;
        height: 60rpx;
      }
    }
    // pics
    .fitem {
      padding: 20rpx 16rpx 10rpx;
      overflow: hidden;
      navigator {
        float: left;
        width: 193rpx;
        height: 188rpx;
        margin-left: 10rpx;
        margin-bottom: 10rpx;
      }
      navigator:nth-child(1) {
        width: 232rpx;
        height: 386rpx;
        margin-left: 0;
      }
      navigator:nth-child(2),
      navigator:nth-child(5) {
        width: 273rpx;
        height: 188rpx;
      }
    }
    &:first-child {
      .fitem {
        navigator {
          width: 233rpx;
        }
      }
    }
  }
  .end {
    text-align: center;
    font-size: 24rpx;
    color: #999;
  }
  .goTop {
    position: fixed;
    bottom: 30rpx;
    /* #ifdef H5 */
    bottom: 65px;
    /* #endif */
    right: 30rpx;

    display: flex;
    justify-content: center;
    align-items: center;
    width: 100rpx;
    height: 100rpx;
    font-size: 48rpx;
    color: #666;
    border-radius: 50%;
    background-color: rgba(255, 255, 255, 0.8);
  }
}
</style>
